package cn.lmxdev.ptms.service.impl;

import cn.lmxdev.ptms.bean.exception.BizException;
import cn.lmxdev.ptms.bean.table.TTopic;
import cn.lmxdev.ptms.dao.ITopicDao;
import cn.lmxdev.ptms.service.ITopicServlet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class TopicServletImpl implements ITopicServlet {
    private final ITopicDao topicDao;

    @Autowired
    public TopicServletImpl(
            ITopicDao topicDao
    ) {
        this.topicDao = topicDao;
    }

    @Override
    @Transactional
    public TTopic get(Long id) {
        return this.topicDao.find(id);
    }

    @Override
    @Transactional
    public long add(TTopic topic) {
        // 插入数据
        if (!this.topicDao.insert(topic))
            throw new BizException("操作失败");

        // 返回ID
        return topic.getId();
    }

    @Override
    @Transactional
    public void remove(Long id) {
        // 删除数据
        if (!this.topicDao.delete(id))
            throw new BizException("删除失败");
    }

    @Override
    @Transactional
    public void edit(TTopic topic) {
        // 查询旧数据
        TTopic oldTopic = this.topicDao.find(topic.getId());

        // 更新属性
        oldTopic.setSpecialtyId(topic.getSpecialtyId())
                .setTitle(topic.getTitle())
                .setSynopsis(topic.getSynopsis())
                .setFile(topic.getFile());

        // 保存数据
        if (!this.topicDao.update(oldTopic))
            throw new BizException("操作失败");
    }

    @Override
    @Transactional
    public long count(TTopic topic) {
        return this.topicDao.count(topic);
    }

    @Override
    @Transactional
    public List<TTopic> list(Long limit, Long page, TTopic topic) {
        return this.topicDao.select(page != null ? (page - 1) * limit : null, limit, topic);
    }

    @Override
    @Transactional
    public long specialtyHeadCount(Long specialtyHeadId, TTopic topic) {
        return this.topicDao.specialtyHeadCount(specialtyHeadId, topic);
    }

    @Override
    @Transactional
    public List<TTopic> specialtyHeadList(Long specialtyHeadId, Long limit, Long page, TTopic topic) {
        return this.topicDao.specialtyHeadSelect(specialtyHeadId, page != null ? (page - 1) * limit : null, limit, topic);
    }

    @Override
    @Transactional
    public long adviserCount(Long adviserId, TTopic topic) {
        return this.topicDao.adviserCount(adviserId, topic);
    }

    @Override
    @Transactional
    public List<TTopic> adviserList(Long adviserId, Long limit, Long page, TTopic topic) {
        return this.topicDao.adviserSelect(adviserId, page != null ? (page - 1) * limit : null, limit, topic);
    }

    @Override
    @Transactional
    public long studentCount(Long studentId, TTopic topic) {
        return this.topicDao.studentCount(studentId, topic);
    }

    @Override
    @Transactional
    public List<TTopic> studentList(Long studentId, Long limit, Long page, TTopic topic) {
        return this.topicDao.studentSelect(studentId, page != null ? (page - 1) * limit : null, limit, topic);
    }
}
